package com.database;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.model.Note;

public class NoteAdapter {
	private static final String DATABASE_NAME = "springpadapp";
	private static final int DATABASE_VERSION = 1;
	private SQLiteDatabase db;
	private DbHelper dbHelper;
	private Context ctx;
	
	
	public static final String TABLE_NAME1="note";
	public static final String KEY_ROWID = "_id";
	private static final String KEY_NAME="name";
	private static final String KEY_ITEM="content";
	private static final String KEY_NOTEBOOKID="notebookid";
	public NoteAdapter(Context ctx){
		this.ctx=ctx;
		
	}
	public NoteAdapter open(){
		dbHelper = new DbHelper(ctx, DATABASE_NAME, null, DATABASE_VERSION);
		db=dbHelper.getWritableDatabase();
		return this;
	}
	public void close(){
		dbHelper.close();
	}
	public long createNote(Note gp){
		Log.d("Note","creating a new note");
		ContentValues initialValues = new ContentValues();
	    initialValues.put(KEY_NAME, gp.getName());
	    initialValues.put(KEY_ITEM, gp.getContent());
	    initialValues.put(KEY_NOTEBOOKID, gp.getNotebookid());
	    Log.d("content",gp.getNotebookid()+"");
	    return db.insert(TABLE_NAME1, null, initialValues);
	}
	public Cursor fetchAllGroups(){
		String colNames[]={KEY_ROWID,KEY_NAME,KEY_ITEM,KEY_NOTEBOOKID};
		dbHelper.onCreate(db);
		return db.query(TABLE_NAME1, colNames, null, null, null, null, null);
	}
	public boolean deleteGroup(long _id){
		return db.delete(TABLE_NAME1, KEY_ROWID+ '=' +_id, null)>0;
	}
	public ArrayList<Note> fetchGroups(){
		ArrayList<Note> list=new ArrayList<Note>();
		Cursor c=fetchAllGroups();
		if(c!=null){
			while(c.moveToNext()){
				Integer id=c.getInt(c.getColumnIndexOrThrow(KEY_ROWID));
				String name=c.getString(c.getColumnIndexOrThrow(KEY_NAME));
				String content=c.getString(c.getColumnIndexOrThrow(KEY_ITEM));
				Integer nid=c.getInt(c.getColumnIndexOrThrow(KEY_NOTEBOOKID));
				Log.d("Note id",id.toString());
				Log.d("Note name",name);
				Log.d("Note content",content);
				Note g=new Note(name,id,content,nid);
				//Note g=new Note(name,id);
				list.add(g);
			}
		}
		c.close();
		return list;
	}
	
	public ArrayList<Note> fetchSelectedNotes(int notebookid){
		ArrayList<Note> list=new ArrayList<Note>();
		Cursor c=fetchAllGroups();
		if(c!=null){
			while(c.moveToNext()){
				Integer id=c.getInt(c.getColumnIndexOrThrow(KEY_ROWID));
				String name=c.getString(c.getColumnIndexOrThrow(KEY_NAME));
				String content=c.getString(c.getColumnIndexOrThrow(KEY_ITEM));
				Integer nid=c.getInt(c.getColumnIndexOrThrow(KEY_NOTEBOOKID));
				if(nid==notebookid)
				{
				Log.d("Note id",id.toString());
				Log.d("Note name",name);
				Log.d("Note content",content);
				Note g=new Note(name,id,content,nid);
				//Note g=new Note(name,id);
				list.add(g);
				}
				else
				{
					
					Log.d("Not",name);
				}
			}
		}
		c.close();
		return list;
	}
	
	public ArrayList<Note> fetchNoteOnName(String notename){
		ArrayList<Note> list=new ArrayList<Note>();
		Cursor c=fetchAllGroups();
		if(c!=null){
			while(c.moveToNext()){
				
				String name=c.getString(c.getColumnIndexOrThrow(KEY_NAME));
				
				if(notename==name)
				{
				
				Log.d("Note name",name);
				
				Note g=new Note();
				g.setName(name);
				//Note g=new Note(name,id);
				list.add(g);
				}
				else
				{
					
					//Log.d("Not",name);
				}
			}
		}
		c.close();
		return list;
	}
}
